/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Penjualan;

import Koneksi.koneksi;
import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import oracle.jdbc.pool.OracleDataSource;

/**
 *
 * @author asus
 */
public class PenjualanController {
    //* Deklarasi variabel

    private Connection conn = null;
    private static PenjualanController penjualanController;

    public PenjualanController(Connection conn) {
        this.conn = conn;
    }

    public static PenjualanController getKoneksi() throws SQLException, FileNotFoundException, IOException {
        koneksi kon = new koneksi();
        if (penjualanController == null) {
            penjualanController = new PenjualanController(koneksi.getDBConnection());
        }
        return penjualanController;
    }

    public int inputDataPenjualan(Penjualan jual) throws SQLException {
        //Statement stmt = null;
        String noPenjualan = jual.getNoPenjualan();
        String waktu = jual.getWaktu();
        String noHp = jual.getNoHp();
        String prov = jual.getProvider();
        int nominal = jual.getNominal();
        int harga = jual.getHarga();

//        String stat=jual.getStatus();

        String sql = "{CALL spInsertPenjualan(?,?,?,?,?,?,?)}";
        CallableStatement call = null;
        call = conn.prepareCall(sql);

        call.setString(1, noPenjualan);
        call.setString(2, waktu);
        call.setString(3, noHp);
        call.setInt(4, nominal);
        call.setString(5, prov);
        call.setInt(6, harga);
        call.registerOutParameter(7, java.sql.Types.INTEGER);
        call.execute();
        int status = call.getInt(7);
        call.close();
        conn.commit();
        return status;
    }

    public Penjualan cariNomor(Penjualan jual) throws SQLException, Exception {
        CallableStatement callst = null;
        String sql = "{CALL CARINOMORJUAL(?,?,?,?,?)}";
        callst = conn.prepareCall(sql);
        callst.setString(1, jual.getNoPenjualan());
        callst.registerOutParameter(2, java.sql.Types.VARCHAR);
        callst.registerOutParameter(3, java.sql.Types.INTEGER);
        callst.registerOutParameter(4, java.sql.Types.VARCHAR);
        callst.registerOutParameter(5, java.sql.Types.INTEGER);
        callst.execute();
        String noHp = callst.getString(2);
        int nom = callst.getInt(3);
        String prov = callst.getString(4);
        int harga = callst.getInt(5);

        jual.setNoHp(noHp);
        jual.setNominal("" + nom);
        jual.setProvider(prov);
        jual.setHarga("" + harga);
        callst.execute();
        callst.close();

        return jual;
    }

    public Penjualan updateDataPenjualan(Penjualan jual) throws SQLException {
        PreparedStatement callst = null;
        //* Store Procedure belum
        String sql = "update penjualanpulsa set nomorhp=(?), provider=(?), nominal=(?),"
                + "hargapenjualan=(?) where notransaksipenjualan=(?)";
        callst = conn.prepareStatement(sql);

        callst.setString(1, jual.getNoHp());
        callst.setString(2, jual.getProvider());
        callst.setInt(3, jual.getNominal());
        callst.setInt(4, jual.getHarga());
        callst.setString(5, jual.getNoPenjualan());
        callst.execute();
        callst.close();
        conn.commit();
        return jual;
    }

     public List<Penjualan> getSisaStok() throws SQLException{
        PreparedStatement state=null;
        ResultSet set=null;
        conn.setAutoCommit(false);
        String sql="select stokakhir from pembelianpulsa where waktu="
                + "(select max(waktu) from pembelianpulsa)";
        state=conn.prepareStatement(sql);
        set = state.executeQuery();
        List<Penjualan>juallist = new ArrayList<Penjualan>();
        while(set.next()){
            Penjualan jual=new Penjualan();
            jual.setStok(set.getLong("stokakhir"));
            juallist.add(jual);
        }
        conn.commit();
        return juallist;


    }
}
